package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Update extends JInternalFrame {
	Connection conn;
	DatabaseTest main;
	
	private JLabel lblNewLabel;
	private JTextField code;
	private JButton btnNewButton;
	private JLabel lblNewLabel_1;
	private JTextField ea;
	private JButton btnNewButton_1;
	private JLabel status;
	private JLabel lblNewLabel_3;
	private JTextField serial;
	private JButton btnNewButton_2;

	public Update(Connection c , DatabaseTest m) {
		super("수정",true, true, true, true);
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 363, 218);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getCode());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getStatus());
		getContentPane().add(getLblNewLabel_3());
		getContentPane().add(getSerial());
		getContentPane().add(getBtnNewButton_2());
		
		this.conn = c;
		this.main = m;
		
	}
	
	public void setData(String s, String c, String e){
		serial.setText(s);
		code.setText(c);
		ea.setText(e);
	}

	public void deleteData(){
		int s = Integer.parseInt(serial.getText());
		String sql = "delete from ptbl where serial=?";
		try{
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, s);
			int cnt = pstmt.executeUpdate();
			pstmt.close();
				
			if( cnt>0){
				if(main.select != null) main.select.setList();
				status.setText("데이터가 삭제되었습니다");
			}else{
				status.setText("삭제할 데이터가 없습니다.");
			}	
			pstmt.close();
			code.setText("");
			ea.setText("");
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("code");
			lblNewLabel.setBounds(12, 43, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(81, 40, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(serial.getText());
					String sql = "select * from ptbl where serial = ?";
					try{
						PreparedStatement pstmt =
							conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						
						ResultSet rs = pstmt.executeQuery();
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText(""+ rs.getInt("ea"));
						}else{
							status.setText("데이터가 없습니다.");
							code.setText("");
							ea.setText("");
							serial.requestFocus();
						}
						
						rs.close();
						pstmt.close();
						
						
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(209, 14, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("ea");
			lblNewLabel_1.setBounds(12, 77, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setBounds(81, 71, 116, 21);
			ea.setColumns(10);
		}
		return ea;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("수정");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(serial.getText());
					String c = code.getText();
					int e = Integer.parseInt(ea.getText());
					String sql = "update ptbl set code=?, ea=? "
							+ " where serial=?";
					try{
						PreparedStatement pstmt =
						  conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, e);
						pstmt.setInt(3, s);
						
						pstmt.executeUpdate();
						pstmt.close();
						
						if(main.select != null) main.select.setList();
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnNewButton_1.setBounds(81, 102, 97, 23);
		}
		return btnNewButton_1;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("검색할 코드를 입력하세요...");
			status.setBounds(12, 164, 323, 15);
		}
		return status;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("serial");
			lblNewLabel_3.setBounds(12, 18, 57, 15);
		}
		return lblNewLabel_3;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setBounds(81, 15, 116, 21);
			serial.setColumns(10);
		}
		return serial;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("삭제");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					deleteData();
					
				}
			});
			btnNewButton_2.setBounds(186, 102, 97, 23);
		}
		return btnNewButton_2;
	}
}
